using System.Collections;
using UnityEngine;
public class SpawnPoint : MonoBehaviour
{
    public GameObject SpawnTarget;
    [Range(1.1f, 10f)]
    public float spawnTimer = 1.1f;

    private SpawnItem m_SpawnItem;
    private Animator m_Animator;
    private int m_Respawning;
    private int m_Spawn;
    #region Public方法

    #endregion

    #region Private方法

    #endregion

    #region Unity方法
    private void Awake()
    {
        m_Animator = GetComponent<Animator>();
        m_Respawning = Animator.StringToHash("Respawning");
        m_Spawn = Animator.StringToHash("Spawn");
        if (SpawnTarget == null)
        {
            m_SpawnItem = GetComponentInChildren<SpawnItem>();
            if (m_SpawnItem != null)
            {
                SpawnTarget = m_SpawnItem.gameObject;
            }
        }
        if (SpawnTarget != null)
        {
            m_SpawnItem = SpawnTarget.GetComponent<SpawnItem>();
            if (m_SpawnItem != null)
            {
                m_SpawnItem.OnSelected += () => { StartCoroutine(Respawn()); };
            }
        }
    }

    private IEnumerator Respawn()
    {
        SpawnTarget.SetActive(false);
        float wait = spawnTimer - 1.1f;
        yield return new WaitForSeconds(wait);
        m_Animator.SetTrigger(m_Respawning);
        yield return new WaitForSeconds(1f);
        m_Animator.SetTrigger(m_Spawn);
        yield return new WaitForSeconds(0.1f);
        SpawnTarget.SetActive(true);
        SpawnTarget.transform.position = this.transform.position;
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, 0.5f);
    }
    #endregion
}
